package com.belf.web.controller.console;

import com.belf.common.annotation.Log;
import com.belf.common.core.controller.BaseController;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.core.domain.entity.SysUser;
import com.belf.common.core.page.TableDataInfo;
import com.belf.common.enums.BusinessType;
import com.belf.common.utils.SecurityUtils;
import com.belf.common.utils.poi.ExcelUtil;
import com.belf.console.domain.AppraiseTasks;
import com.belf.console.service.IAppraiseTasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 评估任务Controller
 * 
 * @author belf
 * @date 2024-07-11
 */
@RestController
@RequestMapping("/console/appraiseTask")
public class AppraiseTasksController extends BaseController
{
    @Autowired
    private IAppraiseTasksService appraiseTasksService;

    /**
     * 查询评估任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody AppraiseTasks appraiseTasks)
    {
        // 管理员拥有所有权限
        if (!SysUser.isAdmin(SecurityUtils.getUserId())){
            appraiseTasks.setCreateBy(getUsername());
        }
        List<AppraiseTasks> list = appraiseTasksService.selectAppraiseTasksList(appraiseTasks);
        long count = appraiseTasksService.selectDataCount(appraiseTasks);
        return getDataTablePost(list,count);
    }

    /**
     * 导出评估任务列表
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:export')")
    @Log(title = "评估任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppraiseTasks appraiseTasks)
    {
        List<AppraiseTasks> list = appraiseTasksService.selectAppraiseTasksList(appraiseTasks);
        ExcelUtil<AppraiseTasks> util = new ExcelUtil<AppraiseTasks>(AppraiseTasks.class);
        util.exportExcel(response, list, "评估任务数据");
    }

    /**
     * 获取评估任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:query')")
    @PostMapping("/getInfo")
    @ResponseBody
    public AjaxResult getInfo(@RequestBody AppraiseTasks appraiseTasks)
    {
        return success(appraiseTasksService.selectAppraiseTasksById(appraiseTasks.getId()));
    }

    /**
     * 新增评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:add')")
    @Log(title = "评估任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestBody AppraiseTasks appraiseTasks)
    {
        appraiseTasks.setCreateBy(getUsername());
        return toAjax(appraiseTasksService.insertAppraiseTasks(appraiseTasks));
    }

    /**
     * 修改评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:edit')")
    @Log(title = "评估任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestBody AppraiseTasks appraiseTasks)
    {
        appraiseTasks.setUpdateBy(getUsername());
        return toAjax(appraiseTasksService.updateAppraiseTasks(appraiseTasks));
    }


    /**
     * 删除评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:remove')")
    @Log(title = "评估任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody AppraiseTasks appraiseTasks)
    {
        return toAjax(appraiseTasksService.deleteAppraiseTasksByIds(appraiseTasks.getIdArray()));
    }

    /**
     * 执行评估任务
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:remove')")
    @Log(title = "评估任务", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public AjaxResult execute(@RequestBody AppraiseTasks appraiseTasks)
    {
        return appraiseTasksService.executeTaskById(appraiseTasks.getId());
    }

    /**
     * 启动调度器
     */
    @PreAuthorize("@ss.hasPermi('console:tasks:remove')")
    @Log(title = "评估任务", businessType = BusinessType.UPDATE)
    @PostMapping("/scheduler")
    public AjaxResult scheduler(@RequestBody AppraiseTasks appraiseTasks)
    {
        return appraiseTasksService.schedulerTask(appraiseTasks.getIdArray());
    }

}
